<?php

namespace App\Http\Controllers\Admin;

use Dotenv\Validator;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller {
    /*
     * @熊猫立方的后台登录
     *
     */

    public function index() {
        return redirect('admin/commodity');
    }

    public function login(Request $request) {
        //判断请求方式
        if ($request->isMethod('post')) {
            $rules = [
                'username' => 'required',
                'password' => 'required'
            ];
            //设置格式错误信息
            $msg = [
                'username.required' => '用户名不能为空',
                'password.required' => '密码不能为空'
            ];
            //进行格式验证
            $validator = \Validator::make($request->all(), $rules, $msg);
            if ($validator->fails()) {
                return redirect('admin/login')->withErrors($validator)->withInput();
            }
            $credentials = ['user_name' => $request->username, 'password' => $request->input('password')];
            //认证成功
            if (Auth::guard('admin')->attempt($credentials, $request->has('remember'))) {
                return redirect('/admin/commodity');
            }
            return back()->with('loginFails', '用户名密码不匹配');
        }
        return view('admin/login');
    }

    public function logout(Request $request) {
        //清空用户信息
        Auth::guard('admin')->logout();
        return redirect('/admin/login');
    }

}
